<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use App\Http\Requests\UserRequest;

class UserController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //$this->middleware('auth');
    }

    /**
     * Show the users list.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */

    public function index()
    {
        $users = User::latest()->paginate();
        return view('dashboard.users.index')->with('datas', $users);
    }

    public function edit(User $user) {
    	return view('dashboard.users.create', ['user' => $user]);
    }

    public function update(UserRequest $request, User $user) {
        $data = $request->all();
        if(empty($data['password']))
        {
            unset($data['password']);
        }
        $user->update($data);
        return redirect()->route('admin.users.index');
    }

    public function destroy(User $user)
    {
        $user->delete();
        return response()->json(['status' => 0]);
    }
}
